import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';

import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { Service } from '../../../server/server';
import { InstitutionService } from '../../services/institution.service';
import { Data } from '@angular/router/src/config';
declare var $: any;

@Component({
  selector: 'app-institution-table',
  templateUrl: './institution-table.component.html',
})
export class InstitutionTableComponent implements OnInit {

  @Input() tableNumber: number;

  seletedIds: string[] = [];

  right: any = {
    "user": {},
    "institution": {}
  };
  constructor(private http: HttpClient, private server: Service, private router: Router, private tableListService: CudTableService, private removeTableService: RemoveTableService, private institutionService: InstitutionService) {
    this.right = $.extend(this.right, server.getRight());
  }

  ngOnInit() {
    this.getTabList(1, 20, "12aa1c87305f4271a90bffeb28315ca2",'');
    this.selectedItemsSubject = this.institutionService.selectedItemsSubject.subscribe((selectedIds: string[]) => {
      //console.log(selectedIds)
      this.seletedIds = selectedIds
    })
    //接受查询结果
    this.institutionsearcheventEmit = this.server.institutionsearcheventEmit.subscribe((value: any) => {
      this.tabVal = value;
      //console.log(this.tabVal)
    });
    //获取机构ID
    this.institutiontreeidnameeventEmit = this.server.institutiontreeidnameeventEmit.subscribe((value: any) => {
      this.institutionId = value.institutionId;
      //console.log(this.institutionId)
    })
  }

  public institutionId:any;

  //取消订阅
  public selectedItemsSubject: any;
  public institutionsearcheventEmit: any;
  public institutiontreeidnameeventEmit :any;
  ngOnDestroy() {
    if (this.selectedItemsSubject || this.institutionsearcheventEmit || this.institutiontreeidnameeventEmit ) {
      this.selectedItemsSubject.unsubscribe();
      this.institutionsearcheventEmit.unsubscribe();
      this.institutiontreeidnameeventEmit.unsubscribe();
    }
  }

  @Output()
  tabeListOutput: EventEmitter<string> = new EventEmitter();

  public tabVal: Datatype[] = [];

  // 表格
  getTabList(pageNumber, pageSize, id,queryCriteria) {
    let url = this.server.baseUrl + 'admin/institutions/getSublevelInfo';
    //let data = new Pagination(pageNumber, pageSize, id);
    let data = {pageNumber:pageNumber,pageSize:pageSize,id:id || "12aa1c87305f4271a90bffeb28315ca2",queryCriteria:queryCriteria}
    this.institutionService.clearSelectedItems();
    this.tableListService.postRequest(url, data).then((data: any) => {
      if(data.result_code == '0000') {
        this.institutionService.tabValSubject.next(data.result)
        this.tabVal = data.result;
        this.tabeListOutput.emit(data.length);
      }
    }).catch();
  }

  

  @Output()
  detailsOutput: EventEmitter<Datatype> = new EventEmitter();

  of(val) {
    $("#of").modal("show");
    //console.log(val)
    this.server.institutionofeventEmit.emit(val);
  }

  // 详情（弹窗），用于展示默认数据
  detailsPopup(tabval) {
    $("#details").modal("show");
    this.outputTabVal(tabval);
    //console.log(tabval)
  }

  // 修改（弹窗），用于展示默认数据#update
  editPopup(tabval) {
    $("#update").modal("show");
    this.outputTabVal(tabval);
  }
  // 每一行的表格信息
  outputTabVal(tabval: Datatype) {
    let tabVal: Datatype = {
      address: tabval.address,
      city: tabval.city,
      contactName: tabval.contactName,
      createDate: tabval.createDate,
      description: tabval.description,
      enabled: tabval.enabled,
      industry: tabval.industry,
      institutionDisplayName: tabval.institutionDisplayName,
      institutionId: tabval.institutionId,
      institutionName: tabval.institutionName,
      institutionOrder: tabval.institutionOrder,
      institutionType: tabval.institutionType,
      jgServerType:tabval.jgServerType,
      updateStatus:tabval.updateStatus,
      lastUpdatedDate: tabval.lastUpdatedDate,
      lastUpdatedUserId: tabval.lastUpdatedUserId,
      nature: tabval.nature,
      parentId: tabval.parentId,
      phone: tabval.phone,
      postcode: tabval.postcode,
      province: tabval.province,
      validDate: tabval.validDate,
    };
    this.detailsOutput.emit(tabVal);
  }

  public isSelectAll: boolean = false;
  public isChose: boolean = false;
  // 全选
  selectAll() {
    this.institutionService.selectAll(this.tabVal)
  }
  isSelected(id: string) {
    return !!this.institutionService.isSeleted(id)
  }
  // 单选（多选）
  chose(bool, functionId, length) {
    this.isSelectAll = this.removeTableService.removeChose(bool, functionId, length);
  }

  // 没有数据时取消勾选全选按钮
  cancelSelectAll() {
    this.isSelectAll = false;
    this.isChose = false;
  }
  selectThis(id: string, val: any) {
    this.institutionService.sendSelectedItems(id)
  }

}

export class Datatype {
  address?: string;
  city?: string;
  contactName?: string;
  createDate?: string;
  createUserId?: string;
  description?: string;
  enabled?: any;
  industry?: string;
  institutionDisplayName?: string;
  institutionId?: string;
  institutionName?: string;
  institutionOrder?: string;
  institutionType?: string;
  jgServerType?:string;
  updateStatus?:string;
  lastUpdatedDate?: string;
  lastUpdatedUserId?: string;
  nature?: string;
  parentId?: string;
  phone?: string;
  postcode?: string;
  province?: string;
  validDate?: string | Date;
}